package com.mec.offload.controller;


import com.mec.offload.model.DistanceInfo;
import com.mec.offload.model.MEC;
import com.mec.offload.model.NodeTerm;
import com.mec.offload.service.DistanceInfoService;
import com.mec.offload.service.MecService;
import com.mec.offload.service.NodeTermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/offload/DistanceInfo")
@CrossOrigin
public class DistanceInfoController{
    @Autowired
    NodeTermService nodeTermService;
    @Autowired
    MecService mecService;
    @Autowired
    DistanceInfoService distanceInfoService;
    //@CrossOrigin(origins = "*")
    @RequestMapping("Init")
    @ResponseBody
    public int InitDistance()
    {
        try{
            List<DistanceInfo> listD=distanceInfoService.getAllDistanceInfo();
            if(listD.size()==0)
            {
                List<NodeTerm> listT=nodeTermService.getAllNodeTerm();
                List<MEC> listM=mecService.getAllMec();
                for(int i=0;i<listT.size();i++)
                    for(int j=0;j<listM.size();j++)
                    {
                        Double distance=Math.sqrt(Math.pow((listT.get(i).getTermX() - listM.get(j).getMecX()), 2) + Math.pow(listT.get(i).getTermY() - listM.get(j).getMecY(), 2));
                        DistanceInfo info = new DistanceInfo();
                        info.setTermId(listT.get(i).getTermId());
                        info.setMecId(listM.get(j).getMecId());
                        info.setDistance(distance);
                        distanceInfoService.addDistanceInfo(info);

                    }
                return 1;
            }
            else return 1;
        }catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("error");
            return -1;
        }
    }
}
